using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class mouseevent : MonoBehaviour
{
    public float lastClickTime = 0f;
    private const float doubleClickTimeMin = 0.01f; // 10毫秒
    private const float doubleClickTimeMax = 0.3f; // 300毫秒

    public Button button;
    private bool isPressed = false;

    // private Vector3 originalScale;
    public float scaleFactor = 1.02f; // 放大倍数，可以在Inspector中调整
    private Vector2 originalSizeDelta;
    private float maxSizeDeltaX;
    private Vector2 currentSizeDelta;
    private bool isBlowUp = false;

    void Start()
    {
        if (button != null)
        {
            originalSizeDelta = button.GetComponent<RectTransform>().sizeDelta;
            // 最多放大到原来的1.2倍
            maxSizeDeltaX = originalSizeDelta.x * 1.2f;
        }
    }

    // 鼠标进入物体时调用
    public void OnMouseDown()
    {
        isPressed = true;
    }

    // 鼠标离开物体时调用
    public void OnMouseUp()
    {
        isPressed = false;
        if (button != null)
        {
            button.GetComponent<RectTransform>().sizeDelta = originalSizeDelta;
        }
    }

    // 定义一个点击事件
    public void OnClick()
    {
        float currentClickTime = Time.time;
        float timeSinceLastClick = currentClickTime - lastClickTime;
        if (timeSinceLastClick > doubleClickTimeMin && timeSinceLastClick < doubleClickTimeMax)
        {
            OnDoubleClick();
        }
        lastClickTime = currentClickTime;
        // Debug.Log("button.GetComponent<RectTransform>().sizeDelta");
        // Transform button = button.GetComponent<Transform>();
        // Debug.Log(button);
        // Debug.Log(button.GetComponent<Transform>().GetComponent<RectTransform>());
    }

    // 定义一个双击事件，两次的点击时间间隔为10~300毫秒
    private void OnDoubleClick()
    {
        Debug.Log("Double click detected!");
        gameObject.GetComponentInParent<Nodes>().doubleClick();
    }

    // Update is called once per frame
    void Update()
    {
        if (button != null)
        {
            // Debug.Log("maxSizeDeltaX");
            // Debug.Log(maxSizeDeltaX);
            currentSizeDelta = button.GetComponent<RectTransform>().sizeDelta;
            // // 如果鼠标按下并且当前大小小于最大大小，则放大按钮
            // Debug.Log("currentSizeDelta.x");
            // Debug.Log(currentSizeDelta.x);

            //是否需要放大
            isBlowUp = currentSizeDelta.x < maxSizeDeltaX;
            // Debug.Log("isBlowUp");
            // Debug.Log(isBlowUp);
            if (isPressed == true && isBlowUp)
            {
                // Debug.Log("isPressed is not false");
                //     Debug.Log(button);
                button.GetComponent<RectTransform>().sizeDelta =
                    button.GetComponent<RectTransform>().sizeDelta * scaleFactor;
            }
        }
    }
}
// 子节点ui组件上触发的onClick事件中如何调用父节点ui组件上触发的事件?
// 如何在物体节点的一个script上,正确的获取到该物体内部的预制体节点?
// 如何在物体节点的一个script上,正确的获取到该物体内部的预制体节点的信息?
